﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogisticsBLL;
using System.Text.RegularExpressions;

namespace WebLosgic.Driver
{
    public partial class AddDriver : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DDlbind();
            }
            
        }

        public void DDlbind()
        {
            TruckBLL tr = new TruckBLL();
            DdlTeam.DataSource = tr.ddlbindCarName();
            DdlTeam.DataTextField = "TeamName";
            DdlTeam.DataValueField = "TeamID";
            DdlTeam.DataBind();
            DdlTeam.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            string phonereg = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
            Regex re = new Regex(phonereg);
            string Name = TxtName.Text;
            string Sex = rblSex.SelectedValue;
            string birth = iptBrith.Value;
            string Phone = TxtPhone.Text;
            string Idcard = TxtIDcard.Text;
            string TruckID =DdlTeam.SelectedValue;
            string State = "1";
            string Remark = TxtRemark.Text;
            string AddTime = DateTime.Now.ToString();
            if(Name=="" && Phone=="" || Idcard=="" && TruckID == "0")
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请完善驾驶员信息')", true);
            }
            if (re.IsMatch(TxtPhone.Text.Trim()) == false)
            {
               
                    Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('手机号码格式不正确')", true);
            }
            if (Idcard.Length == 18) { 
            DriverBLL dr = new DriverBLL();
             
            int result = dr.AddDriver(Name, Sex, birth, Phone,Idcard, TruckID, State, Remark, AddTime);
            if (result > 0)
            {
                    string name = Session["names"].ToString();
                    string Behavor = "" + name + ": 添加驾驶员";
                    int TypeId = 11;
                    int UserID = Convert.ToInt32(Session["UserIDs"]);
                    string Parameters = @"" + Name + "," + Sex + "," + State + "," + Phone;
                    string ProcName = "insert into Driver values(@Name,@Sex,@Brith)";
                    HttpRequest request = HttpContext.Current.Request;
                    string IP = "192.168.0.1";
                    int IsExcePion = 0;
                    int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                    if (a > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');location='DriverXinxi.aspx';", true);
                    }
                    
            }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "aa", "alert('身份证号格式不正确')", true);
            }
          
            
        }

        protected void BtnReset_Click(object sender, EventArgs e)
        {
            TxtName.Text = "";
            TxtPhone.Text = "";
            TxtRemark.Text = "";
            DdlTeam.SelectedValue = "0";
            TxtIDcard.Text = "";
            iptBrith.Value = "";
            //TxtAddTime.Text = "";
            rblSex.SelectedValue = "0";
            //RblState.SelectedValue = "0";
                                                                         
        }
    }
}